﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

[LuaCallCSharp]
public enum UILayerIndex{
    UILayer=0,
    MiddleLayer,
    PopLayer,
    EffectLayer,
    SpareLay,
    TopLayer,
}

[LuaCallCSharp]
public class UIMrg : Singleton<UIMrg>
{

    private Dictionary<string, GameObject> windowList = null;
    private string[] LayerNames = new string[] { 
        "UILayer", "MiddleLayer", "PopLayer", "EffectLayer","SpareLay","TopLayer"
    };
    private List<RectTransform> layerRectList = new List<RectTransform>();
    private Camera camera;
    private GameObject root;
    private Canvas canvas;



    public Camera UICamera { get { return camera; } }
    
    
    public void Initialized(string canvasName= "Canvas")
    {
        this.root = GameObject.Find(canvasName);
        this.canvas = root.GetComponent<Canvas>();
        if(this.canvas != null)this.camera = this.canvas.worldCamera;
        if (this.camera == null) this.camera = Camera.main;
        windowList=new Dictionary<string, GameObject>();
        for (int i = 0; i < LayerNames.Length; i++)
        {
            GameObject layer = this.canvas.transform.Find(LayerNames[i])?.gameObject;
            if (layer == null)
            {
                layer = new GameObject(LayerNames[i]);
                layer.transform.SetParent(this.canvas.transform);
            }
            Vector3 pos = new Vector3(0, 0, -1 * (i + 1));
            RectTransform rectTransform = layer.GetComponent<RectTransform>();
            if (rectTransform == null) rectTransform = layer.AddComponent<RectTransform>();
            this.SetTo(rectTransform);
            //rectTransform.gameObject.layer = LayerMask.NameToLayer("UI");
            layerRectList.Add(rectTransform);
        }
        DontDestroyOnLoad(this.root);
    }
    public void SetTo(RectTransform rectTransform)
    {
        if (rectTransform != null)
        {
            rectTransform.localPosition = Vector3.zero;
            rectTransform.localScale = Vector3.one;
            rectTransform.localRotation = Quaternion.identity;
            rectTransform.anchorMin = Vector2.zero;
            rectTransform.anchorMax = Vector2.one;
            rectTransform.pivot = new Vector2(0.5f, 0.5f);
            rectTransform.anchoredPosition3D = Vector2.zero;// pos;
            rectTransform.sizeDelta = Vector2.zero;
        }
        
    }
    public RectTransform GetLayer(int uILayer)
    {
        return layerRectList[uILayer];
    }
    public RectTransform GetLayer(UILayerIndex uILayer)
    {
        return layerRectList[uILayer.GetHashCode()];
    }
    Dictionary<string,GameObject> map_list = new Dictionary<string,GameObject>();
    /// <summary>
    /// 显示一个UI
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path"></param>
    /// <param name="uILayer"></param>
    /// <param name="comp"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public T Show<T>(string path,UILayerIndex uILayer=UILayerIndex.UILayer,params object[] values) where T : UIComponent
    {
        RectTransform paretn = this.GetLayer(uILayer);
        GameObject ui_node = null;
        if (this.map_list.ContainsKey(path))
        {
            this.Hide(path);
        }
        GameObject node_prefab = LoadMrg.Instance.Load<GameObject>(path);
        if (node_prefab != null)
        {
            ui_node=GameObject.Instantiate(node_prefab);
            this.map_list.Add(path, ui_node);
            RectTransform rectTransform = ui_node.GetComponent<RectTransform>();
            if (rectTransform != null)
            {
                rectTransform.SetParent(paretn.transform);
                this.SetTo(rectTransform);
                T new_comp = ui_node.GetComponent<T>();
                if (!new_comp) new_comp = ui_node.AddComponent<T>();
                new_comp.Open(values);
                return new_comp;
            }
           
        }
        return null;
    }
    public void Hide(string path,float delayed = 0)
    {
        if (this.map_list.ContainsKey(path))
        {
            GameObject ui_node = this.map_list[path];
            GameObject.Destroy(ui_node, delayed);
            this.map_list.Remove(path);
        }
    }

}
